package zhihui.mobile.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import zhihui.mobile.app.MyApplication;
import zhihui.mobile.config.Config;
import zhihui.mobile.manager.pay.PayManager;
import zhihui.mobile.ui.express.BaseEventBus;

/**
 * Created by 张公铭 on 2017/9/10 0010.
 */

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Config.WX_APP_ID);
        api.registerApp(Config.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (baseResp.errCode == 0) {
                Toast.makeText(this, "支付成功！", Toast.LENGTH_SHORT).show();
                PayManager.wx_pay = 1;
                MyApplication.finishActivity(WXPayEntryActivity.class);
                EventBus.getDefault().post(new BaseEventBus("finish_pay"));
            } else {
                Toast.makeText(this, "支付失败！", Toast.LENGTH_SHORT).show();
                PayManager.wx_pay = -1;
                MyApplication.finishActivity(WXPayEntryActivity.class);
                EventBus.getDefault().post(new BaseEventBus("finish_pay"));
            }
        }
    }

}
